package com.basic.controller;


import com.basic.util.FileUtil;
import com.code.domain.AjaxResult;
import com.code.domain.CommonResult;
import com.code.entity.User;
import com.code.mapper.UserMapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

//文件上传
//跨域请求
@Controller
@Slf4j
@CrossOrigin
@ApiOperation(value = "上传",notes = "上传")
@RequestMapping(value = "/util/upload")
public class UploadController {

    @Autowired
    private UserMapper userMapper;
    @Resource
    HttpServletRequest request;

    //定义常量
    private static final String ITEM_PICTURE_PATH = "/Users/guanfeng/Desktop/image/";


    @ApiOperation(value = "图片上传",notes = "图片上传")
    @PostMapping("/importPicture")
    public @ResponseBody String importPicture(@RequestParam("file") MultipartFile file, Model model) throws FileNotFoundException {
        //获得文件的后缀
        String filename = UUID.randomUUID() + Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        //获取文件存储路径
        File filepath = new File(ITEM_PICTURE_PATH + filename);
        try {
            //存文件
            file.transferTo(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost/mall-basic-service/temp-image/"+filename;
    }



    @ApiOperation(value = "上传图片",notes = "上传图片")
    @PostMapping("/toUpload")
    public @ResponseBody String uploadAvatar(@RequestParam("file") MultipartFile file, Model model) {
        // 如果图片为空，显示错误信息
        if ((file.getOriginalFilename().isEmpty())) {
            model.addAttribute("error", "文件不能为空！！！");
            return "upload";
        } else {
            User user = new User();
            // 获取图片的名字
            String fileName = file.getOriginalFilename();
            // 图片存放的文件夹地址
//            String filePath = "/Users/guanfeng/Desktop/springCloud/gitee/test-mall/mall-react/src/assets/img/";
            String filePath = request.getSession().getServletContext().getRealPath("imgupload/");
            // 文件夹地址 + 名字 = 图片的路径
            String fileAddress = filePath + fileName;
            try {
                // 图片上传的工具类
                // 参数一：图片的编码格式的字节数组
                // 参数二：图片存放的文件夹地址
                // 参数三：图片的名字
                FileUtil.uploadFile(file.getBytes(), filePath, fileName);
                // 把图片路径写入数据库
//                user.setHeadPortraitUrl(fileAddress);
//                userMapper.uploadInfo(user);
            } catch (Exception e) {
                e.printStackTrace();
            }
            model.addAttribute("user", user);
            return fileAddress;
        }
    }
}

